package com.example.testspringes.sutao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;

/**
 * @Description: //TODO
 * @ProjectName: testspringes
 * @Package: com.example.testspringes.sutao
 * @Author: FX
 * @CreateDate: 2019/4/2 12:20
 * @Version: 1.0
 * Copyright: Copyright (c) 2019
 */
public class Test2 {
    public static final Logger LOGGER = LoggerFactory.getLogger(Test2.class);
    public static final String D_AA_XLSX = "D:\\aa.xlsx";
    public static final String SUTAO_TXT = "D:\\sutao.txt";


    public static void inportExcl() {
        try(InputStream in = new FileInputStream(D_AA_XLSX);) {
            File file = new File(D_AA_XLSX);
            List<List<Object>> listob = ExcelUtils.getBankListByExcel(in, file.getName());
            System.out.println(listob);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < listob.size(); i++) {
                List<Object> ob = listob.get(i);
                String s = String.valueOf(ob.get(0));
                stringBuilder.append("，").append(s);

            }
            writeFile(stringBuilder.deleteCharAt(0).toString());
            File sutaoTxt = new File(SUTAO_TXT);
            if (sutaoTxt.exists()) {
                LOGGER.info("------内容写入成功------");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入TXT文件
     */
    public static void writeFile(String str) {
        try {
            // 相对路径，如果没有则要建立一个新的output.txt文件
            File writeName = new File(SUTAO_TXT);
            // 创建新文件,有同名的文件的话直接覆盖
            if(!writeName.exists()){
                boolean newFile = writeName.createNewFile();
                if (!newFile) {
                    LOGGER.error("文件创建失败");
                }else{
                    LOGGER.error("文件不存在，创建成功");
                }

            }
            try (FileWriter writer = new FileWriter(writeName,true);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
                //out.write("我会写入文件啦1\r\n"); // \r\n即为换行

                out.write(str);
                out.flush(); // 把缓存区内容压入文件
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
